package me.greyliu.kuai.service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncodeService {
    //编码密码
    public static String encodePwd(String pwd)  {
        // 创建MessageDigest对象，指定使用MD5算法
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        // 将字符串转换为字节数组
        byte[] inputBytes = pwd.getBytes();
        // 执行MD5加密
        byte[] hashBytes = md.digest(inputBytes);

        // 将字节数组转换为十六进制字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.length; i++) {
            sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    }
}
